<template>
	<view>
		<view class="search_box level_align flex_between tp-bgf tp-ptb0lr30">
			<view class="search_v level_align tp-bgf">
				<view class="tp-mr20" @click="typeShow = !typeShow">
					{{searchType.name}}
				</view>
				<uni-icons @click="typeShow = !typeShow" type="arrowdown" size="16" color="#666"></uni-icons>
				<text class="line"></text>
				<image class="search-img tp-mr15" src="../../static/imgs/icons/search-icon.png" mode=""></image>
				<input class="flex-1" type="text" v-model="searchVal" @input="search" placeholder="请输入关键字">
				<view v-show="typeShow" class="type-box tp-br8">
					<view @click="selectType(item)" class="type-item tp-h64 flex align_center" v-for="(item,index) in typeList" :key="index">
						{{item.name}}
					</view>
				</view>
			</view>
			<view class="search center_combo tp-h88" @click="searchJumps">
				搜索
			</view>
		</view>

		<view class="history_title_box level_align flex_between tp-h88 tp-mt20" v-if="homeSearchHistoryList.length">
			<view class="">
				历史记录
			</view>
			<view class="level_align">
				<view class="adders_img tp-mr20">
					<image class="tp-img32" src="../../static/imgs/icons/del.png" mode=""></image>
				</view>
				<view class="" @click="delAll">
					清除历史
				</view>
			</view>
		</view>
		<view class="history_list flex flex_wrap " v-if="homeSearchHistoryList.length">
			<view class="tag tp-bgf tp-mb20 tp-mr20 tp-br8" @click="clickHistorySearch(item)" v-for="(item,index) in homeSearchHistoryList"
			 :key="index">
				<view class="level_align">
					<view class="">
						{{item}}
					</view>
					<view class="close-icon level_align" @click.stop="delTag(index)">
						<uni-icons type="closeempty" size="24" color="#999"></uni-icons>
					</view>
				</view>

			</view>
		</view>
		<view class="history_title_box level_align flex_between tp-h88 tp-mt20">
			<view class="">
				热门推荐
			</view>

		</view>
		<view class="history_list flex flex_wrap ">
			<view class="tag tp-bgf tp-mb20 tp-mr20 tp-br8" @click="search({value: item})" v-for="(item,index) in HistoryList"
			 :key="index">
				<view class="level_align">
					<view class="">
						{{item}}
					</view>

				</view>

			</view>
		</view>

		<!-- 图片 S -->
		<view class="tp-m0a tu1 tp-mt20">
			<image class="" :src="www + 'imgs/snap/tu1.png'" mode=""></image>
		</view>
		<view class="level_align flex_between tp-ptb0lr30 tp-mt20">
			<view class="tu2 ">
				<image class="" :src="www + 'imgs/snap/tu2.png'" mode=""></image>
			</view>
			<view class="tu2 ">
				<image class="" :src="www + 'imgs/snap/tu3.png'" mode=""></image>
			</view>
		</view>
		<view class="tp-m0a tu1 tp-mt20">
			<image class="" :src="www + 'imgs/snap/tu4.png'" mode=""></image>
		</view>
		<!-- 图片 E -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchVal: '', // 搜索的内容
				HistoryList: [], // 搜索历史
				searchType: {
					name: '酒店',
					url: '/pagesSecond/hotel/hotelHome?searchVal=',
					tabBar: false,
				},
				typeShow: false,
				typeList: [{
						name: '酒店',
						url: '/pagesSecond/hotel/hotelHome?searchVal=',
						tabBar: false,
					}, {
						name: '景区',
						url: '/pagesSecond/scenic/scenicHome?searchVal=',
						tabBar: false,
					}, {
						name: '旅游',
						url: '/pagesSecond/travel/travelHome?searchVal=',
						tabBar: false,
					}, {
						name: '攻略',
						url: '/pagesSecond/travel/travelHome?searchVal=',
						tabBar: true,
					},
					// {
					// 	name: '目的地',
					// 	url: '/pagesSecond/destination/destinationDetail',
					// 	tabBar: false,
					// },
				],
				homeSearchHistoryList: [],
			};
		},
		onLoad(opt) {

			// this.getCityList()
		},
		onShow() {
			if (uni.getStorageSync('homeSearchHistoryList')) {
				this.homeSearchHistoryList = uni.getStorageSync('homeSearchHistoryList');
			}
		},
		methods: {
			// 选择搜索类型
			selectType(item) {
				this.searchType = item
				this.typeShow = !this.typeShow
			},
			// 页面跳转
			searchJumps() {
				if (!this.searchVal) return
				if (this.homeSearchHistoryList.indexOf(this.searchVal) == -1) {
					this.homeSearchHistoryList.unshift(this.searchVal)
				}
				if (this.homeSearchHistoryList.length > 10) {
					this.homeSearchHistoryList.pop()
				}
				uni.setStorageSync('homeSearchHistoryList', this.homeSearchHistoryList)
				if (this.searchType.tabBar) {
					uni.switchTab({
						url: this.searchType.url + this.searchVal
					})
				} else {
					uni.navigateTo({
						url: this.searchType.url + this.searchVal
					})
				}
			},
			// 获取城市列表
			getCityList() {
				this.$http.get(this.$api.citySearchIndex + '?keyWords=' + this.searchVal).then(res => {
					if (res.code === 200) {
						this.list = res.data.cityMap
						this.tuijianList = res.data.tuijianList
					}
				})
			},
			// 点击历史搜素
			clickHistorySearch(val) {
				this.searchVal = val
				// console.log(this.searchVal);
				this.searchJumps()
			},
			// 搜索
			search(e) {
				this.searchVal = e.detail.value.trim()
				// console.log(this.searchVal);
			},
			// 选择地址
			selectAdders(val) {

			},
			// 删除单个记录
			delOne(index) {
				this.homeSearchHistoryList.splice(index, 1)
				uni.setStorageSync('homeSearchHistoryList', this.homeSearchHistoryList)
			},
			// 删除全部
			delAll() {
				this.homeSearchHistoryList = []
				uni.removeStorageSync('homeSearchHistoryList');
			},

		}
	}
</script>

<style lang="scss" scoped>
	.search_box {
		margin-bottom: 4rpx;
		background-color: #00B4FF;

		.search_v {
			position: relative;
			width: 619rpx;
			height: 56rpx;
			border-radius: 28rpx;
			padding-left: 30rpx;
		}

		.search {
			width: 70rpx;
			margin-left: 20rpx;
			font-size: 30rpx;
			color: #fff;
		}

		.line {
			height: 36rpx;
			width: 2rpx;
			background: #EAEAEA;
			margin: 0 20rpx;
		}

		.search-img {
			width: 28rpx;
			height: 28rpx;
		}

		.type-box {
			position: absolute;
			top: calc(100% + 2rpx);
			left: 20rpx;
			right: 0;
			width: 140rpx;
			background: #FFFFFF;
			color: #333333;
			z-index: 1;

			.type-item {
				border-left: 2rpx solid rgba(245, 245, 245, 1);
				border-right: 2rpx solid #F5F5F5;
				border-bottom: 2rpx solid #F5F5F5;
				padding-left: 20rpx;
			}
		}
	}

	.history_title_box {
		padding: 0 40rpx;
		font-size: 32rpx;
	}

	.history_list {
		padding: 0 40rpx;

		.tag {
			height: 70rpx;
			line-height: 70rpx;
			padding: 0 20rpx;
			font-size: 28rpx;
			border: 0;
			display: inline-block;
			font-family: PingFang SC;
			font-weight: 500;
			color: rgba(51, 51, 51, 1);

			.close-icon {
				width: 20rpx;
				height: 20rpx;
				margin-left: 10rpx;
				margin-right: 10rpx;
			}
		}
	}

	.tu1 {
		width: 690rpx;
		height: 222rpx;
	}

	.tu2 {
		width: 335rpx;
		height: 128rpx;
	}
</style>
